/*
	Copyright (C) 2013 Pontificia Universidad Catolica del Peru
	contact: http://avatar.inf.pucp.edu.pe/realidad-aumentada/
	address contact: Av. Universitaria 1801, San Miguel, Lima-Peru.
	
	This file is part of UbicAR PUCP.

	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with This program.  If not, see <http://www.gnu.org/licenses/>.
*/
/*
	Authors: Alvaro Roger Agüero Sánchez, Rodolfo Javier Galvez Meza
	Pontificia Universidad Catolica del Peru
	Departamento de Ingeniería
*/

package org.grupoavatar.guiaubicua.dialogos;

import java.util.ArrayList;

import org.grupoavatar.guiaubicua.R;
import org.grupoavatar.guiaubicua.auxiliares.Item;
import org.grupoavatar.guiaubicua.mix.Marker;
import org.grupoavatar.guiaubicua.mix.MixContext;
import org.grupoavatar.guiaubicua.mix.MixState;
import org.grupoavatar.guiaubicua.mix.data.DataSource;
import org.grupoavatar.guiaubicua.mix.data.DataSource.DATASOURCE;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.TextView;

public class Filtro_Subcategorias extends AlertDialog.Builder {
	//public static POI_Locked_Actions poiLockedActions = null;
	public static Activity mActivity;
	public static ArrayList<Marker> obstruidos;
	public static MixContext context;

	public Filtro_Subcategorias(DATASOURCE datasource, final Context context, final Marker punto, Activity actividad,final MixContext ctx,final MixState state) {		
		super(context);
		//this.context = ctx;
		//poiLockedActions = this;
		mActivity = actividad;
		this.setTitle(MixContext.getDataSourceName(datasource));	
//		final CharSequence[] items = punto.getPuntosSuperpuestos();
		final Item []items = DataSource.getSubCat(datasource);
		ListAdapter adapter = new ItemAdapter( context, items );
		
		
		this.setSingleChoiceItems(adapter, -1, new OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int item) {
				
			}
		});
		AlertDialog alert = create();
  	  	alert.show();
		//this.show();
		
		/*
		this.setItems(items, new DialogInterface.OnClickListener() {
 	       public void onClick(DialogInterface dialog, int item) {
 	    	   
// 	    	   int duration = Toast.LENGTH_SHORT;
// 	    	   Toast toast = Toast.makeText(context, "Le diste a un punto superpuesto!", duration);
// 	    	   toast.show();
 	    	   punto.abre_dialogo(item);
 	       }
 	   });
 	   */
	}
	
	@SuppressWarnings("rawtypes")
	static class ItemAdapter extends ArrayAdapter {

		private static final int RESOURCE = R.layout.listapoi_row;
		private LayoutInflater inflater;

	    static class ViewHolder {
	        TextView nameTxVw;
	    }

		@SuppressWarnings("unchecked")
		public ItemAdapter(Context context, Item[] objects)
		{
			super(context, RESOURCE, objects);
			inflater = LayoutInflater.from(context);
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent)
		{
			ViewHolder holder;

			if ( convertView == null ) {
				// inflate a new view and setup the view holder for future use
				convertView = inflater.inflate( RESOURCE, null );

				holder = new ViewHolder();
				holder.nameTxVw = (TextView) convertView.findViewById(R.id.txtTitleListPOI);
				convertView.setTag( holder );
			}  else {
				// view already defined, retrieve view holder
				holder = (ViewHolder) convertView.getTag();
			}

			Item cat = (Item) getItem( position );
			if ( cat == null ) {
				//Log.e( TAG, "Invalid category for position: " + position );
			}
			holder.nameTxVw.setText( cat.getText() );
			holder.nameTxVw.setCompoundDrawables( cat.getImg(), null, null, null );

			return convertView;
		}
	}
}
